﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Com.Rivyer.DAL
{
	/// <summary>
	/// 数据访问类:ipaddressDal
	/// </summary>
	public partial class ipaddressDal
	{
		public ipaddressDal()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("id", "tb_ipaddress"); 
		}


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from tb_ipaddress");
			strSql.Append(" where id="+id+" ");
			return DbHelperSQL.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Com.Rivyer.Model.ipaddressModel model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.ip != null)
			{
				strSql1.Append("ip,");
				strSql2.Append("'"+model.ip+"',");
			}
			if (model.usedperson != null)
			{
				strSql1.Append("usedperson,");
				strSql2.Append("'"+model.usedperson+"',");
			}
			if (model.department != null)
			{
				strSql1.Append("department,");
				strSql2.Append("'"+model.department+"',");
			}
			if (model.organization != null)
			{
				strSql1.Append("organization,");
				strSql2.Append("'"+model.organization+"',");
			}
			if (model.usedinternet != null)
			{
				strSql1.Append("usedinternet,");
				strSql2.Append(""+model.usedinternet+",");
			}
			if (model.description != null)
			{
				strSql1.Append("description,");
				strSql2.Append("'"+model.description+"',");
			}
			if (model.macaddress != null)
			{
				strSql1.Append("macaddress,");
				strSql2.Append("'"+model.macaddress+"',");
			}
			if (model.productdate != null)
			{
				strSql1.Append("productdate,");
				strSql2.Append("'"+model.productdate+"',");
			}
			if (model.modifydate != null)
			{
				strSql1.Append("modifydate,");
				strSql2.Append("'"+model.modifydate+"',");
			}
			strSql.Append("insert into tb_ipaddress(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			strSql.Append(";select @@IDENTITY");
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Com.Rivyer.Model.ipaddressModel model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update tb_ipaddress set ");
			if (model.ip != null)
			{
				strSql.Append("ip='"+model.ip+"',");
			}
			if (model.usedperson != null)
			{
				strSql.Append("usedperson='"+model.usedperson+"',");
			}
			if (model.department != null)
			{
				strSql.Append("department='"+model.department+"',");
			}
			if (model.organization != null)
			{
				strSql.Append("organization='"+model.organization+"',");
			}
			if (model.usedinternet != null)
			{
				strSql.Append("usedinternet="+model.usedinternet+",");
			}
			if (model.description != null)
			{
				strSql.Append("description='"+model.description+"',");
			}
			if (model.macaddress != null)
			{
				strSql.Append("macaddress='"+model.macaddress+"',");
			}
			else
			{
				strSql.Append("macaddress= null ,");
			}
			if (model.productdate != null)
			{
				strSql.Append("productdate='"+model.productdate+"',");
			}
			else
			{
				strSql.Append("productdate= null ,");
			}
			if (model.modifydate != null)
			{
				strSql.Append("modifydate='"+model.modifydate+"',");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where id="+ model.id+"");
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_ipaddress ");
			strSql.Append(" where id="+id+"" );
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_ipaddress ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Com.Rivyer.Model.ipaddressModel GetModel(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" id,ip,usedperson,department,organization,usedinternet,description,macaddress,productdate,modifydate ");
			strSql.Append(" from tb_ipaddress ");
			strSql.Append(" where id="+id+"" );
			Com.Rivyer.Model.ipaddressModel model=new Com.Rivyer.Model.ipaddressModel();
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ip"]!=null && ds.Tables[0].Rows[0]["ip"].ToString()!="")
				{
					model.ip=ds.Tables[0].Rows[0]["ip"].ToString();
				}
				if(ds.Tables[0].Rows[0]["usedperson"]!=null && ds.Tables[0].Rows[0]["usedperson"].ToString()!="")
				{
					model.usedperson=ds.Tables[0].Rows[0]["usedperson"].ToString();
				}
				if(ds.Tables[0].Rows[0]["department"]!=null && ds.Tables[0].Rows[0]["department"].ToString()!="")
				{
					model.department=ds.Tables[0].Rows[0]["department"].ToString();
				}
				if(ds.Tables[0].Rows[0]["organization"]!=null && ds.Tables[0].Rows[0]["organization"].ToString()!="")
				{
					model.organization=ds.Tables[0].Rows[0]["organization"].ToString();
				}
				if(ds.Tables[0].Rows[0]["usedinternet"]!=null && ds.Tables[0].Rows[0]["usedinternet"].ToString()!="")
				{
					model.usedinternet=int.Parse(ds.Tables[0].Rows[0]["usedinternet"].ToString());
				}
				if(ds.Tables[0].Rows[0]["description"]!=null && ds.Tables[0].Rows[0]["description"].ToString()!="")
				{
					model.description=ds.Tables[0].Rows[0]["description"].ToString();
				}
				if(ds.Tables[0].Rows[0]["macaddress"]!=null && ds.Tables[0].Rows[0]["macaddress"].ToString()!="")
				{
					model.macaddress=ds.Tables[0].Rows[0]["macaddress"].ToString();
				}
				if(ds.Tables[0].Rows[0]["productdate"]!=null && ds.Tables[0].Rows[0]["productdate"].ToString()!="")
				{
					model.productdate=DateTime.Parse(ds.Tables[0].Rows[0]["productdate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["modifydate"]!=null && ds.Tables[0].Rows[0]["modifydate"].ToString()!="")
				{
					model.modifydate=DateTime.Parse(ds.Tables[0].Rows[0]["modifydate"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,ip,usedperson,department,organization,usedinternet,description,macaddress,productdate,modifydate ");
			strSql.Append(" FROM tb_ipaddress ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,ip,usedperson,department,organization,usedinternet,description,macaddress,productdate,modifydate ");
			strSql.Append(" FROM tb_ipaddress ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM tb_ipaddress ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from tb_ipaddress T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
	}
}

